﻿using FreesenseSubAIS.Common;
using FreesenseSubAIS.Common.Helpers;
using FreesenseSubAIS.Core;
using FreesenseSubAIS.SocketModel;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FreesenseSubAIS.NewUI
{
    public partial class SplashScreenForm : Form
    {
        private Action _dismsg;
        private ThreadPart _loadControl;
        public SplashScreenForm()
        {
            InitializeComponent();
        }
        private void LoadResources()
        {
            Device.LoadingTime = DateTime.Now;
            _loadControl = new ThreadPart();
            var judgerLoadparamDone = new Judger(() => Device.defaultProduct != null) { Name = "加载参数" };
            var judgerInitDeviceDone = new Judger(() => Device.DeviceEntityControl != null) { Name = "初始化硬件" };
            var judgerInitTcpDone = new Judger(() => Server.Intrance.SerMgr != null) { Name = "开始启动TCP服务" };

            DisMsg("开始加载参数");
            var result = judgerLoadparamDone.Sure(_loadControl.CancelToken, _loadControl.ContinueEvent, _loadControl.StopEvent, 100);
            DisMsg(string.Format("加载参数{0}", result ? "成功" : "失败"));

            DisMsg("开始初始化硬件");
            var result1 = judgerInitDeviceDone.Sure(_loadControl.CancelToken, _loadControl.ContinueEvent, _loadControl.StopEvent, 3000);
            result1 = result1 && Device.AllReady();
            DisMsg(string.Format("初始化硬件{0},Info:{1} ,{2}", result1 ? "成功" : "失败", Device.DeviceEntityControl.ETSMsg, Device.DeviceEntityControl.CameraMsg));

            DisMsg("开始启动TCP服务");
            var result2 = judgerInitTcpDone.Sure(_loadControl.CancelToken, _loadControl.ContinueEvent, _loadControl.StopEvent, 1000);
            result2 = result2 && Server.Intrance.OnServer;
            DisMsg(string.Format("启动TCP服务{0}", result2 ? "成功" : "失败"));
        }
        private void DisMsg(string msg)
        {
            _dismsg = new Action(() =>
            {
                listBox1.Items.Add(msg);
                listBox1.SelectedIndex = listBox1.Items.Count - 1;
            });
            listBox1.Invoke(_dismsg);
            Utiles.SaveLoadingLog(msg, Device.LoadingTime.ToString ("yyyyMMdd_HH_mm_ss_ff"));
        }
        private void Loadparam()
        {
            DisMsg("加载参数");
            int timeout = 10;
            while (timeout-- > 0)
            {
                if (Device.defaultProduct != null)
                {
                    DisMsg("加载参数完成");
                    break;
                }
                Thread.Sleep(10);
            }
            if (timeout < 0)
                DisMsg("加载参数出错");
        }

        private void SplashScreenForm_Load(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() => LoadResources());
            //Task.Factory.StartNew(() =>
            //{
            //    Thread.Sleep(100);
            //    _loadControl.StopEvent.Set();
            //    DisMsg("已暂停！");
            //    Thread.Sleep(3000);
            //    _loadControl.ContinueEvent.Set();
            //    DisMsg("已继续！");
            //});

        }
    }
}
